package Set;

import java.util.TreeSet;

public class TreeCompareTest {
    public static void main(String[] args) {
        TreeSet<Stu>treeSet =new TreeSet<>();
        treeSet.add(new Stu("ton",22));
        treeSet.add(new Stu("son",32));
        treeSet.add( new Stu("lon",12));
        treeSet.add( new Stu("jon",2));
        treeSet.add( new Stu("jeon",22));
        System.out.println(treeSet.size());//1
        for (Stu s:
            treeSet ) {
            System.out.println(s.name);//ton
            System.out.print(s+"\t");//2  12  22 32
        }
    }
}
class Stu implements Comparable<Stu>{
    String name;
    Integer age;

    public Stu(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Stu o) {
        System.out.println("Compare被Tree回调");
        //升序
        if (this.age <o.age ) {//比this小,this就要靠前
            return  -1;
        }else if(this.age>o.age){//比this大,this就要靠后
            return  1;
        }
        return 0;//结果一样,代表去重
    }
//    @Override
//    public int compareTo(Stu o) {
//        System.out.println("Compare被Tree回调");
//        //降序
//        if (this.age <o.age ) {//比this小,this就要靠前
//            return  1;
//        }else if(this.age>o.age){//比this大,this就要靠后
//            return  -1;
//        }
//        return 0;//结果一样,代表去重
//    }
}